package com.h3c.tj_nsh;

import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.Handler;

import com.h3c.tj_nsh.Model.ProcessDefinitionList;

import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

/**
 * Time: 2016/5/11.
 * Author:Chen  .
 */
public class MyApplication extends Application {
    private List<Activity> activitys = new LinkedList<Activity>();
    private List<Service> services = new LinkedList<Service>();
    private static int mainThreadId;
    private static Context context;
    private static Handler handler;
    private int currentUserLevel;
    private ProcessDefinitionList mProcessDefinitionList;

    @Override
    public void onCreate() {
        super.onCreate();

        // 初始化context对象，context对象使用的非常多
        context = getApplicationContext();
        // 获取主线程的handler 相当于获取主线程的消息队列
        handler = new Handler();
        //读取本地保存的流程模板
        loadProcessDefinitionList();
    }


    public void addActivity(Activity activity) {
        activitys.add(activity);
    }

    public void removeActivity(Activity activity) {
        activitys.remove(activity);
    }

    public void addService(Service service) {
        services.add(service);
    }

    public void removeService(Service service) {
        services.remove(service);
    }

    public void closeApplication() {
        closeActivitys();
        closeServices();
    }

    private void closeActivitys() {
        ListIterator<Activity> iterator = activitys.listIterator();
        while (iterator.hasNext()) {
            Activity activity = iterator.next();
            if (activity != null) {
                activity.finish();
            }
        }
    }

    private void closeServices() {
        ListIterator<Service> iterator = services.listIterator();
        while (iterator.hasNext()) {
            Service service = iterator.next();
            if (service != null) {
                stopService(new Intent(this, service.getClass()));
            }
        }
    }

    public static Context getContext() {
        return context;
    }

    public static Handler getHandler() {
        return handler;
    }

    public static int getMainThreadId() {
        return mainThreadId;
    }

    /*  public Account getCurrentAccount() {
          if (account == null) {
              AccountDao dao = new AccountDao(this);
              account = dao.getCurrentAccount();
          }
          return account;
      }*/
    public void setUserLevel(int level) {
        this.currentUserLevel = level;
    }

    public int getUserLevel() {

        return currentUserLevel;
    }

    /***
     * 设置流程模板信息列表对象，并保存到文件系统
     *
     * @param list
     */
    public void setProcessDefinitionList(ProcessDefinitionList list) {
        mProcessDefinitionList = list;
        deleteFile("ProcessDefinitionList.obj");

        if (mProcessDefinitionList == null) {
            return;
        }

        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(openFileOutput("ProcessDefinitionList.obj", MODE_PRIVATE));
            oos.writeObject(list);
        } catch (IOException e) {
            e.printStackTrace();
            deleteFile("ProcessDefinitionList.obj");
        } finally {
            try {
                if (oos != null) {
                    oos.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    /***
     * 从文件系统获取流程模板信息列表对象
     */
    private void loadProcessDefinitionList() {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(openFileInput("ProcessDefinitionList.obj"));
            mProcessDefinitionList = (ProcessDefinitionList) ois.readObject();
        } catch (IOException e) {
            e.printStackTrace();
            mProcessDefinitionList = null;
            deleteFile("ProcessDefinitionList.obj");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
            mProcessDefinitionList = null;
            deleteFile("ProcessDefinitionList.obj");
        } catch (ClassCastException e) {
            e.printStackTrace();
            mProcessDefinitionList = null;
            deleteFile("ProcessDefinitionList.obj");
        } finally {
            try {
                if (ois != null) {
                    ois.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }

    public ProcessDefinitionList getProcessDefinitionList() {
        return mProcessDefinitionList;
    }
}
